//
//  WriteFile.m
//  MyGameSetting
//
//  Created by Hunk on 10-5-7.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "WriteFile.h"


@implementation WriteFile

-(id)init
{
	if((self = [super init]))
	{
		writeData = [[NSMutableData alloc] init];
	}
	return self;
}

// Close File
-(void)CloseFile:(NSString*)filePath
{
	NSArray *arrayPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	
	NSString *documentsDirectory = [arrayPath objectAtIndex:0];
	
	//文件路径或者直接用~表示当前应用的目录
	NSString *strFileName = [NSString alloc];
	strFileName = filePath;
	
	NSString *strFilePath = [documentsDirectory stringByAppendingPathComponent:strFileName]; 
	
	[writeData writeToFile:strFilePath atomically:YES];
	
	writeData = nil;
	[writeData release];
}

// Write int data to file
-(void)WriteInt:(int) data
{
	[writeData appendBytes:&data length:sizeof(data)];
}

// Write float data to file
-(void)WriteFloat:(float)data
{
	[writeData appendBytes:&data length:sizeof(data)];
}

// Write string data to file
-(void)WriteString:(NSString*)strData
{
	NSString* _strData = [[NSString alloc] init];
	
	_strData = strData;
	
	[writeData appendData:[_strData dataUsingEncoding:NSUTF8StringEncoding]];
}

// Write data to file
-(void)WriteData:(NSString*)data size:(int)size
{
	[writeData appendBytes:data length:size];
}

// Delete File
-(void)DeleteFile:(NSString*)filePath
{
	NSArray* arrayPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	
	NSString* documentsDirectory = [arrayPath objectAtIndex:0];
	
	NSFileManager* fileManager = [NSFileManager defaultManager];
	
	NSString* strFileName = [[NSString alloc] init];
	strFileName = filePath;
	
	NSString* strFilePath = [documentsDirectory stringByAppendingPathComponent:strFileName]; 

	[fileManager removeItemAtPath:strFilePath error:NULL];
}

-(void)dealloc
{
	if(writeData)
	{
		writeData = nil;
		[writeData release];
	}
	
	[super dealloc];
}

@end
